<?php

namespace common\models\fenhuiartic;

use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use common\models\fenhuicat\Fenhuicat;
use common\models\fenhuigrade\Fenhuigrade;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "fenhuiartic".
 *
 * @property integer $id
 * @property string $title
 * @property string $subhead
 * @property string $intro
 * @property string $img
 * @property string $cover
 * @property string $author
 * @property string $comefrom
 * @property string $link
 * @property string $content
 * @property string $seocontent
 * @property string $seokeyword
 * @property string $isrecom
 * @property integer $status
 * @property integer $push
 * @property integer $push_status
 * @property integer $created_at
 * @property integer $updated_at
 * @property string $fc_id
 * @property string $optiontag
 * @property integer $front_show
 * @property string $fg_id
 */
class Fenhuiartic extends \yii\db\ActiveRecord
{
    const STATUS_DELETED = 0;
    const STATUS_ACTIVE = 10;
    private $_statusLabel;
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
            ],
        ];
    }
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'fenhuiartic';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title','fc_id'], 'required'],
            [['intro', 'content'], 'string'],
            [['status', 'push', 'push_status', 'updated_at'], 'integer'],
            [['title', 'subhead', 'img', 'author', 'comefrom', 'link', 'fg_id','seocontent', 'seokeyword', 'isrecom', 'cover'], 'string', 'max' => 255],
            [['title'], 'unique'],
            [['created_at'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'title' => Yii::t('app', '标题'),
            'subhead' => Yii::t('app', '副标题'),
            'intro' => Yii::t('app', '简介'),
            'img' => Yii::t('app', '图片'),
            'author' => Yii::t('app', '作者'),
            'comefrom' => Yii::t('app', '来源'),
            'link' => Yii::t('app', '文章信息外链'),
            'content' => Yii::t('app', '详细介绍'),
            'seocontent' => Yii::t('app', 'SEO描述'),
            'seokeyword' => Yii::t('app', 'SEO关键字'),
            'isrecom' => Yii::t('app', '是否推荐'),
            'status' => Yii::t('app', '状态'),
            'push' => Yii::t('app','是否百度推送'),
            'push_status' => Yii::t('app','百度推送状态'),
            'created_at' => Yii::t('app', '创建时间'),
            'updated_at' => Yii::t('app', '更新时间'),
            'fc_id' => Yii::t('app', '栏目分类'),
            'fg_id' => Yii::t('app', '分会名称'),
        ];
    }
    public function getFenhuicat()
    {
        return $this->hasOne(Fenhuicat::className(), ['id' => 'fc_id']);
    }

    public static function getArrayFenhuicat()
    {
        return ArrayHelper::map(Fenhuicat::find()->all(), 'id', 'name');
    }

    public function getFenhuigrade()
    {
        return $this->hasOne(Fenhuigrade::className(), ['id' => 'fg_id']);
    }

    public static function getArrayFenhuigrade()
    {
        return ArrayHelper::map(Fenhuigrade::find()->where(['status'=>10])->all(), 'id', 'name');
    }


    public function getBaidustatus($status)
    {
        return ($status==1)?"已推送":"未推送";
    }
    public function getStatus($status)
    {
        return ($status==10)?"已启用":"停用";
    }
    /**
     * @inheritdoc
     */
    public function getStatusLabel()
    {
        if ($this->_statusLabel === null) {
            $statuses = self::getArrayStatus();
            $this->_statusLabel = array_key_exists($this->status,self::getArrayStatus()) ? $statuses[$this->status] : '无';
        }
        return $this->_statusLabel;
    }
    /**
     * @inheritdoc
     */
    public static function getArrayStatus()
    {
        return [
            self::STATUS_ACTIVE => Yii::t('app', 'STATUS_ACTIVE'),
            self::STATUS_DELETED => Yii::t('app', 'STATUS_INACTIVE'),

        ];
    }
}
